
import nw from "nw-lafjs";
import {ok} from "@/utils/responseUtil";

export default async function (ctx: FunctionContext) {
  let body = ctx.body
  //1.设置默认值即可
  if (nw.util.isNull(body.page)){
    body.page=1
  }
  if (nw.util.isNull(body.limit)){
    body.limit = 10
  }
  //构建返回结果
  let result = {
    "records": [],
    "total": 0,
    "size": body.limit,
    "current": 1,
    "pages": 0
  }
  //2.查询即可
  let whereJson = {deleted:0}
  if (!nw.util.isNull(body.username)){
    // @ts-ignore
    whereJson.username = body.username
  }
  let count = await nw.db.count({
    dbName:"admin",
    whereJson:whereJson
  })

  if (count==0){
    result.total = count
    return ok(result,"查询成功")
  }
  //分页查询结构
  // @ts-ignore
  let res = await nw.db.select({
    dbName:"admin",
    getCount:true,
    pageIndex:body.page,
    pageSize:body.limit,
    whereJson:whereJson,
    sortArr:[
      // @ts-ignore
      { "name": "createTime","type": "desc" }
    ]
  })
  console.log(res)

  result.records = res.rows.map(val=>{
    return nw.util.deleteObjectKeys(val,["phone","wareId","deleted","token","loginTime"])
  })
  result.total = count
  result.current = body.page
  result.size = body.limit
  result.pages =    Math.ceil(result.total / result.size)

  return ok(result,"查询成功")

}
